// import 'dart:ffi';
// import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/user_prop_model/collection_can_transfer_page_model.dart';
import 'package:yysk_app/pages/nft_pages/danation/danation_confirm_widget.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/widget/button_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';
import 'package:oktoast/oktoast.dart';
import 'danation_choose_goods_controller.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';

class GoodsModel {
  String label;
  String code;
  bool isChoose;
  GoodsModel({required this.label, required this.code, required this.isChoose});
}

class DanationChooseGoodsPage extends GetView<DanationChooseGoodsController> {
  static const path = '/danationChooseGoods';
  DanationChooseGoodsPage({super.key});

  final ScrollController scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;
    return PopScope(
      canPop: false,
      child: Scaffold(
        bottomNavigationBar: SafeArea(top: false,child: Container(
          padding: EdgeInsets.fromLTRB(20, 10, 10, 20),
          child: Obx(
                () => (Flex(
              direction: Axis.horizontal,
              children: [
                Expanded(
                  flex: 1,
                  child: RichText(
                    text: TextSpan(
                      style: DefaultTextStyle.of(context).style,
                      children: <TextSpan>[
                        TextSpan(
                          text: '已选中：',
                          style: TextStyle(
                            color: Color(0xFF222222),
                            fontSize: 14.px,
                          ),
                        ),
                        TextSpan(
                          text: '${controller.num}',
                          style: TextStyle(
                            color: Color(0xFF222222),
                            fontSize: 24.px,
                          ),
                        ),
                        TextSpan(
                          text: ' 条',
                          style: TextStyle(
                            color: Color(0xFF222222),
                            fontSize: 14.px,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(2),
                    ),
                    width: 200.px,
                    height: 54.px,
                    child: ButtonWidget(
                      label: '批量转赠',
                      width: 200.px,
                      height: 53.px,
                      fontSize: 16.px,
                      imageBg: 'static/images/batch_btn_bg.png',
                      contentType: ContentType.other,
                      paddingText: EdgeInsets.only(left: 16.px),
                      fontColor: AppColors.backgroundColorBlack,
                      onPressed: () {
                        List<dynamic> list = [];
                        String url = '';
                        int collectionId = 0;
                        for (var element in controller.transferList) {
                          if (element.isSelect == true) {
                            list.add(element.id);
                            url = controller.url.value;
                            collectionId =
                                int.tryParse(controller.collectionId.value) ??
                                    0;
                          }
                        }
                        if (list.isEmpty) {
                          showToast('请选择批量转赠');
                          return;
                        }
                        Get.toNamed(
                          DanationConfirmWidget.path,
                          arguments: {
                            "list": list,
                            'getTokentype': '1',
                            'url': url,
                            'collectionId': collectionId,
                            "type": controller.type.value,
                            "transferTypeWay": 1,
                          },
                        );
                      },
                    ),
                  ),
                ),
              ],
            )),
          ),
        )),
        body: Stack(
          children: [
            SizedBox(
              width: context.width,
              child: ImageAssetWidget(
                width: 375.px,
                height: 200.px,
                imageUrl: 'static/images/setting/give_top_bg@2x.png',
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top: top),
              child: Obx(
                () => Column(
                  children: [
                    CustomAppBar2(
                      title: '批量转赠',
                      context: context,
                      beforBack: () {
                        Navigator.of(context).pop();
                      },
                    ),
                    Text(
                      '${controller.drawerCount.value}',
                      style: TextStyle(fontSize: 0),
                    ),
                    _topInfoWidget(context),
                    Expanded(
                      child: RefreshAndScrollWidget(
                        onRefresh: _handleRefresh,
                        scrollController: controller.scrollController,
                        itemBuilder: (item, index) => _listItem(item, index),
                        itemCount: controller.transferList.length,
                        items: controller.transferList,
                      ),
                      // _listViewWidget(context, controller)
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _topInfoWidget(BuildContext context) {
    return Column(
      children: [
        Container(
          alignment: Alignment.center,
          height: 73.px,
          width: 334.px,
          margin: EdgeInsets.fromLTRB(20, 20, 20, 10),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('static/images/bg_bg.png'),
              fit: BoxFit.fitWidth,
            ),
            borderRadius: BorderRadius.circular(10),
            // border: Border.all(
            //   color: AppColors.backgroundColor.withAlpha(50),
            //   width: 3,
            // ),
            // boxShadow: [
            //   BoxShadow(
            //     color: AppColors.backgroundColorBlack.withAlpha(25),
            //     offset: const Offset(0, 14),
            //     blurRadius: 24,
            //     spreadRadius: 0,
            //   ),
            // ],
          ),

          child: Row(
            children: [
              SizedBox(width: 7.px),
              LazyImage(
                imageUrl: controller.url.value,
                boxFit: BoxFit.cover,
                width: 58.px,
                height: 58.px,
              ),
              SizedBox(width: 25.px),
              Text(
                controller.name.value,
                style: TextStyle(
                  color: AppColors.backgroundColorBlack,
                  fontSize: 16.px,
                ),
              ),
            ],
          ),
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Padding(
                padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                child: Text(
                  '藏品编号',
                  style: TextStyle(
                    color: AppColors.backgroundColorBlack,
                    fontSize: 16.px,
                  ),
                ),
              ),
              SizedBox(
                child: Text(
                  '买入价',
                  style: TextStyle(
                    color: AppColors.backgroundColorBlack,
                    fontSize: 16.px,
                  ),
                ),
              ),
              SizedBox(
                width: 35,
                height: 35,
                child: IconButton(
                  onPressed: () {
                    controller.selectAll();
                  },
                  icon: Image.asset(
                    'static/images/setting/give_all_choose@2x.png',
                  ),
                ),
              ),
            ],
          ),
        ),

        Divider(color: AppColors.lineGreyED, thickness: 1),
      ],
    );
  }

  Future<void> _handleRefresh() async {
    controller.getList();
    controller.num.value = 0;
    return;
  }

  Widget _listItem(CollectionCanTransferPageItem item, index) {
    return GestureDetector(
      onTap: () {
        // if(controller.num.value==20 &&item.isSelect==false){
        //   showToast('最大20条');
        //   return;
        // }
        // // showToast('111');
        // item.isSelect = !item.isSelect;
        // controller.drawerCount.value++;
        // controller.updateNumberAction();
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
        margin: EdgeInsets.fromLTRB(10, 9, 10, 0),
        height: 52.px,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20.px),
          border: Border.all(color: Colors.white12),
          color: AppColors.backgroundColorLightGrey,
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          spacing: 5,
          children: [
            SizedBox(
              width: 70.px,
              child: Text(
                '${item.tokenId} ',
                style: TextStyle(
                  color: AppColors.backgroundColorBlack,
                  fontSize: 14.px,
                  overflow: TextOverflow.ellipsis,
                ),
                textAlign: TextAlign.start,
              ),
            ),
            SizedBox(
              child: Text(
                '￥${item.buyPrice}',
                style: TextStyle(
                  color: AppColors.backgroundColorBlack,
                  fontSize: 12.px,
                  overflow: TextOverflow.ellipsis,
                ),
                textAlign: TextAlign.left,
              ),
            ),
            SizedBox(
              width: 35.px,
              height: 35.px,
              child: IconButton(
                onPressed: () {
                  if (controller.num.value == 20 && item.isSelect == false) {
                    showToast('最大选择20条');
                    return;
                  }
                  var maxCount = 0;
                  var chooseNum = 0;
                  for (var element in controller.transferList) {
                    if (element.isSelect == true) {
                      if (maxCount < 20) {
                        maxCount += 1;
                        controller.drawerCount.value++;
                      } else if (maxCount == 20 &&
                          controller.transferList[index].isSelect) {
                        controller.transferList[index].isSelect =
                            !item.isSelect;
                        controller.drawerCount.value++;
                      } else {
                        showToast('超出最大值20');
                        controller.drawerCount.value++;
                        return;
                      }
                    }
                  }
                  controller.drawerCount.value++;

                  controller.transferList[index].isSelect = !item.isSelect;
                  for (var element in controller.transferList) {
                    if (element.isSelect == true) {
                      chooseNum++;
                    }
                  }
                  controller.num.value = chooseNum;
                  // showToast('${controller.num.value}');
                  controller.drawerCount.value++;
                },
                icon: Image.asset(
                  item.isSelect
                      ? 'static/images/setting/choose_selected@2x.png'
                      : 'static/images/setting/choose_nomal@2x.png',
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
